package com.zdxlz.fcmp.upms.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdxlz.fcmp.common.core.util.KeyGenerator;
import com.zdxlz.fcmp.upms.api.entity.ExternalPlatformConfig;
import com.zdxlz.fcmp.upms.api.mapper.ExternalPlatformConfigMapper;
import com.zdxlz.fcmp.upms.api.service.IExternalPlatformConfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统对外部平台提供的凭证信息 服务实现类
 * </p>
 *
 * @author: aman
 * @since 2025-02-01
 */
@Service
@AllArgsConstructor
public class ExternalPlatformConfigServiceImpl extends ServiceImpl<ExternalPlatformConfigMapper, ExternalPlatformConfig> implements IExternalPlatformConfigService {

    private final ExternalPlatformConfigMapper externalPlatformConfigMapper;

    @Override
    public IPage<ExternalPlatformConfig> getConfigWithPage(Page page, ExternalPlatformConfig entity) {
        Page<ExternalPlatformConfig> pageInfo = new Page<>(page.getCurrent(), page.getSize());

        LambdaQueryWrapper<ExternalPlatformConfig> queryWrapper = new LambdaQueryWrapper<>();

        if (null != entity) {
            queryWrapper.like(StrUtil.isNotBlank(entity.getPlatformName()), ExternalPlatformConfig::getPlatformName, entity.getPlatformName());
        }

        return externalPlatformConfigMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public ExternalPlatformConfig getConfigByAppId(String appId) {
        return externalPlatformConfigMapper.selectOne(Wrappers.<ExternalPlatformConfig>lambdaQuery().eq(ExternalPlatformConfig::getAppId, appId));
    }

    @Override
    public String generateKey() {
        return KeyGenerator.generateKey(32);
    }
}
